package com.pingancar.creditmanage.rest;

import com.pingancar.creditmanage.action.auth.AdminAuthPassport;
import com.pingancar.creditmanage.pojo.FeedbackPojo;
import com.pingancar.creditmanage.pojo.PaservicePojo;
import com.pingancar.creditmanage.pojo.ServicetypePojo;
import com.pingancar.creditmanage.pojo.ShopinfoPojo;
import com.pingancar.creditmanage.rest.model.BaseResult;
import com.pingancar.creditmanage.rest.model.ResultModel;
import com.pingancar.creditmanage.rest.model.ServiceDetail;
import com.pingancar.creditmanage.rest.model.ShopDetail;
import com.pingancar.creditmanage.service.AdminServiceImpl;
import com.pingancar.creditmanage.service.QueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.util.List;

/**
 * Created by SilverStone-WW on 2015/3/2.
 */
@RestController
@RequestMapping("/rest/admin/")
@AdminAuthPassport
public class AdminController {
    @Autowired
    AdminServiceImpl adminService;
    @Autowired
    QueryService queryService;

    @RequestMapping("cashlist")
    public BaseResult cashlist (Integer offset, Integer len, Boolean cashed) {
        BaseResult baseResult = new BaseResult();
        List l = adminService.cashlist(offset, len, cashed);
        baseResult.setData(l);
        baseResult.setStatus(0);
        return baseResult;
    }

    @RequestMapping("cashlistcount")
    public ResultModel<Long> countOfCashlist (Integer offset, Integer len, Boolean cashed) {
        ResultModel<Long> res = new ResultModel<Long>();
        Long c = adminService.countOfCashlist(cashed);
        res.setData(c);
        res.setStatus(0);
        return res;
    }

    @RequestMapping("shoplist")
    public BaseResult shoplist (Integer offset, Integer len, String name) {
        BaseResult baseResult = new BaseResult();
        List l = queryService.getAllShopList(offset, len, name);
        baseResult.setData(l);
        baseResult.setStatus(0);
        return baseResult;
    }

    @RequestMapping("shoplistcount")
    public ResultModel<Long> countOfShoplist (String name) {
        ResultModel<Long> res = new ResultModel<Long>();
        Long c = queryService.getCountOfAllShopList(name);
        res.setData(c);
        res.setStatus(0);
        return res;
    }

    @RequestMapping("shop/{id}")
    public ResultModel<ShopDetail> shopDetail(@PathVariable Integer id) {
        ResultModel<ShopDetail> res = new ResultModel<ShopDetail>();
        res.setStatus(-1);

        ShopinfoPojo shop = queryService.getShopinfoById(id);
        if (shop == null)
            return res;

        List<PaservicePojo> services = queryService.getServiceListByType(id, -1);
        ShopDetail detail = new ShopDetail(shop, services);

        res.setStatus(0);
        res.setData(detail);

        return res;
    }

    @RequestMapping("shop/modify")
    public BaseResult modifyShopDetail(ShopinfoPojo shopinfoPojo) {
        BaseResult res = new BaseResult();

        if (adminService.modifyShop(shopinfoPojo)) {
            res.setStatus(0);
        } else {
            res.setStatus(-1);
        }

        return res;
    }

    @RequestMapping("service/{id}")
    public ResultModel<ServiceDetail> serviceDetail(@PathVariable Integer id) {
        ResultModel<ServiceDetail> res = new ResultModel<ServiceDetail>();
        res.setStatus(-1);

        PaservicePojo service = queryService.getServiceById(id);
        if (service == null)
            return res;

        List<ServicetypePojo> type = queryService.getServicetype();
        ServiceDetail detail = new ServiceDetail(service, type);

        res.setStatus(0);
        res.setData(detail);

        return res;
    }

    @RequestMapping("service/modify")
    public ResultModel<PaservicePojo> modifyServiceDetail(Integer id, String tag1, Integer creditcost, Integer type) {
        ResultModel<PaservicePojo> res = new ResultModel<PaservicePojo>();

        PaservicePojo s = adminService.modifyService(id, tag1, creditcost, type);

        if (s != null) {
            res.setStatus(0);
            res.setData(s);
        } else {
            res.setStatus(-1);
        }

        return res;
    }

    @RequestMapping("service/add")
    public ResultModel<PaservicePojo> addServiceDetail(Integer shopid, String tag1, Integer creditcost, Integer type) {
        ResultModel<PaservicePojo> res = new ResultModel<PaservicePojo>();
        PaservicePojo s = adminService.addService(shopid, tag1, creditcost, type);

        if (s != null) {
            res.setStatus(0);
            res.setData(s);
        } else {
            res.setStatus(-1);
        }

        return res;
    }

    @RequestMapping("service/type/list")
    public ResultModel<List<ServicetypePojo>> listServiceType() {
        ResultModel<List<ServicetypePojo>> res = new ResultModel<List<ServicetypePojo>>();
        List<ServicetypePojo> type = queryService.getServicetype();
        res.setStatus(0);
        res.setData(type);
        return res;
    }

    @RequestMapping("service/stop")
    public ResultModel<PaservicePojo> stopService(Integer id) {
        ResultModel<PaservicePojo> resultModel = new ResultModel<PaservicePojo>();
        resultModel.setStatus(-1);

        PaservicePojo paservicePojo = adminService.stopService(id);
        if (paservicePojo == null)
            return resultModel;

        resultModel.setStatus(0);
        resultModel.setData(paservicePojo);
        return resultModel;
    }

    @RequestMapping("feedbacklistcount")
    public ResultModel<Long> countOfFeedbacklist () {
        ResultModel<Long> res = new ResultModel<Long>();
        Long c = adminService.countOfFeedbacklist();
        res.setData(c);
        res.setStatus(0);
        return res;
    }

    @RequestMapping("feedbacklist")
    public ResultModel<List<FeedbackPojo>> feedbackList (Integer offset, Integer len) {
        ResultModel<List<FeedbackPojo>> resultModel = new ResultModel<List<FeedbackPojo>>();
        List<FeedbackPojo> l = adminService.feedbackList(offset, len);
        resultModel.setData(l);
        resultModel.setStatus(0);
        return resultModel;
    }


    @RequestMapping("feedback/{id}")
    public ResultModel<FeedbackPojo> feedbackDetail(@PathVariable Integer id) {
        ResultModel<FeedbackPojo> resultModel = new ResultModel<FeedbackPojo>();
        resultModel.setStatus(-1);
        FeedbackPojo feedbackPojo = adminService.feedbackDetail(id);
        if (feedbackPojo == null)
            return resultModel;

        resultModel.setStatus(0);
        resultModel.setData(feedbackPojo);

        return resultModel;
    }
}
